$(function () {
    /*初始化商品属性名Table*/
    initialProAttrNameTable();
    /*初始化商品属性值Table*/
    initialProAttrValTable();
    /*初始化添加和修改的模态框*/
    initialAddAndUpdateModel();
    /*初始化属性类型下拉*/
    initialProAttrTypeSelect();

});

function initialProAttrNameTable() {
    var oTable = new TableInit();
    oTable.Init('proAttrNameTab', '/proAttr/getAllPro');
}

function initialProAttrValTable() {
    var oTable = new TableInit();
    oTable.Init('proAttrValTab', '/proAttr/getProAttrValPager');
}

/*格式化商品属性类别*/
function fmtCategory(value) {
    var categoryStr = "";
    if (value == 0) {
        categoryStr = "隐形";
    } else if (value == 1) {
        categoryStr = "显形";
    }
    return categoryStr;
}

/*格式化商品属性名状态*/
function fmtAttrNameState(value, row) {
    var stateStr = "";
    if (value == 0) {
        stateStr = "<input name='state' checked='checked' onchange='updateAttrNameState(this," + row.proAttrNameId + "," + row.proAttrState + ")' type='checkbox' class='iswitch iswitch-secondary'/>";
    } else if (value == 1) {
        stateStr = "<input name='state' onchange='updateAttrNameState(this," + row.proAttrNameId + "," + row.proAttrState + ")' type='checkbox' class='iswitch iswitch-secondary'/>";
    }
    return stateStr;
}

/*修改商品属性名状态*/
function updateAttrNameState(obj, proAttrNameId, proAttrState) {
    console.info(proAttrNameId + "==>" + proAttrState)
    var state = proAttrState;
    var stateStr = "";
    if (state == 1) {
        stateStr = "启用";
        state = 0;
        $(obj).prop("checked", false);
    } else if (state == 0) {
        stateStr = "停用";
        $(obj).prop("checked", true);
        state = 1;
    }
    swal({
        title: "是否修改状态?",
        text: "您确定要修改商品属性状态" + stateStr + "吗？",
        type: "warning",
        showCancelButton: true,
        cancelButtonText: '取消',
        allowOutsideClick: false,//点击警告框外不关闭警告框
        confirmButtonText: '确定',
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33'
    }).then(function (isConfirm) {
        if (isConfirm) {
            $.post(basePath+"/proAttr/updateProAttrNameState",{"proAttrNameId":proAttrNameId,"proAttrState":state},function (msg) {
                if (msg > 0) {
                    toastr.success("商品属性名"+stateStr+"成功！")
                } else {
                    toastr.error("修改商品属性名"+stateStr+"失败！")
                }
                $("#proAttrNameTab").bootstrapTable("refresh");
            },'text')
        }
    })
}

/*格式化操作按钮*/
function fmtOperate() {
    var desc = "<a class='btn btn-xs btn-success edit' href='javascript:void(0);'><i class='glyphicon glyphicon-eye-open'></i>&nbsp;编辑</a>";
    var dele = "<a class='btn btn-xs btn-danger dele' href='javascript:void(0);'><i class='glyphicon glyphicon-trash'></i>&nbsp;删除</a>";
    return desc + dele;
}

/*绑定操作事件*/
window.optAttrNameEvents = {
    "click .edit": function (e, value, row) {
        updateAttrName(row);
    },
    "click .dele": function (e, value, row) {
        // deleRole(row.roleId);
    }
};

/*修改商品属性名*/
function updateAttrName(row) {
    $("#proAttrNameForm").find("form").setForm(row);
    $("#proAttrNameForm").dialog({
        title: '编辑属性名',
        btnValue: '编辑',
        type: 'update',
        btnBind: function () {
            var params = $("#proAttrNameForm").find("form").serialize();
            $.post(basePath + "/proAttr/updateProAttrName", params, function (msg) {
                if (msg > 0) {
                    toastr.success("修改商品属性名成功！")
                } else {
                    toastr.error("修改商品属性名失败！")
                }
                $("#proAttrNameTab").bootstrapTable("refresh");
                $("#proAttrNameForm").dialog("close")
            }, 'text');
        }
    });
    $("#proAttrNameForm").dialog("open");
}

/*初始化商品属性名添加修改模态框*/
function initialAddAndUpdateModel() {
    $("#proAttrNameForm").dialog({});
}

/*初始化属性类型下拉列表*/
function initialProAttrTypeSelect() {
    $.getJSON(basePath + "/proAttr/getAllProAttrType", function (result) {
        $(result).each(function () {
            var $op = $("<option></option>").appendTo($("select[name='proAttrTypeId']"));
            $op.html(this.proAttrTypeName);
            $op.attr("value", this.proAttrTypeId);
        })
    })
}

/*商品属性的添加*/
function proAttrNameAdd() {
    $("#proAttrNameForm").find("form").resetForm();
    $("#proAttrNameForm").dialog({
        title: '添加属性名',
        btnValue: '添加',
        type: 'update',
        btnBind: function () {
            var params = $("#proAttrNameForm").find("form").serialize();
            $.post(basePath + "/proAttr/addProAttrName", params, function (msg) {
                if (msg > 0) {
                    toastr.success("添加商品属性名编号为：" + msg)
                } else {
                    toastr.error("添加商品属性名失败！")
                }
                $("#proAttrNameTab").bootstrapTable("refresh");
                $("#proAttrNameForm").dialog("close")
            }, 'text');
        }
    })
    $("#proAttrNameForm").dialog("open");
}

/*商品属性的修改*/
